package com.qms.tubu.activity.scenic;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qms.tubu.R;
import com.qms.tubu.activity.BaseActivity;
import com.qms.tubu.adapter.ModuleAdapter;
import com.qms.tubu.decoration.GridItemDecoration;
import com.qms.tubu.widget.LoadMoreRecyclerView;


/**
 * Created by Mad on 2015/7/3.
 */
public class ModuleDetailActivity extends BaseActivity implements LoadMoreRecyclerView.OnLoadMoreListener{
    private View mToolbarBg;
    private int toolbarHeight;
    private LoadMoreRecyclerView mRecyclerView;
    private GridLayoutManager mLayoutManager;
    private ModuleAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module_detail);
        mToolbar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        findView();
        initView();
        setToolbarMarginTop();
    }
    private void findView()
    {
        mToolbarBg=this.findViewById(R.id.toolbar_bg_view);
        mRecyclerView= (LoadMoreRecyclerView) this.findViewById(R.id.module_recycler);
    }
    private void initView()
    {
        mToolbarBg.setAlpha(0);
        mLayoutManager=new GridLayoutManager(this,2);
        mAdapter=new ModuleAdapter(this,mLayoutManager,true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.addItemDecoration(new GridItemDecoration(getResources().getDimensionPixelOffset(R.dimen.grid_item_space),
                getResources().getDimensionPixelOffset(R.dimen.grid_item_margin)));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setOnLoadMoreListener(this);
        mRecyclerView.addOnScrollListener(new OnScrollListener());
    }

    @Override
    public void onLoadMore() {

    }

    private void setToolbarMarginTop()
    {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        toolbarHeight=mToolbarBg.getLayoutParams().height;
        if(Build.VERSION.SDK_INT>18) {
            if (resourceId > 0) {
                int statusBarHeight=getResources().getDimensionPixelOffset(resourceId);
                FrameLayout.LayoutParams toolbarParams= new FrameLayout.LayoutParams(mToolbar.getLayoutParams());
                toolbarParams.setMargins(0,statusBarHeight,0,0);
                mToolbar.setLayoutParams(toolbarParams);
                toolbarHeight+=statusBarHeight;
                FrameLayout.LayoutParams bgParams=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, toolbarHeight);
                mToolbarBg.setLayoutParams(bgParams);
            }
        }
    }
    private class OnScrollListener extends RecyclerView.OnScrollListener{
        private int scrollY;
        public OnScrollListener()
        {
            scrollY=0;
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            scrollY=scrollY+dy;
            int height=mAdapter.getHeadHolder().getImage().getMeasuredHeight();
            Log.e("height",scrollY+"");
            if(scrollY<height-toolbarHeight)
            {
                mToolbarBg.setAlpha(0);
            }
            else if(scrollY>height)
            {
                mToolbarBg.setAlpha(1);
            }
            else
            {
                float length=scrollY+toolbarHeight-height;
                mToolbarBg.setAlpha(length/toolbarHeight);
            }
        }
    }
}
